package com.yunxiao.cloud.sharding.algorithm;



import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;

import java.util.Collection;

/**
 * @description: 精确分片算法
 * @date: 2019/12/25 10:45
 * @author: yunxiao
 * @version: 1.0
 */
public class IdShardingAlgorithm implements PreciseShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        Long id = preciseShardingValue.getValue();
        for (String name : collection) {
            if (name.endsWith(id % collection.size() + "")) {
                return name;
            }
        }
        throw new IllegalArgumentException();
    }
}
